package com.zhangmeng.online.exam.service.impl;

import com.zhangmeng.online.exam.dao.*;
import com.zhangmeng.online.exam.entity.*;
import com.zhangmeng.online.exam.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author zm
 * @date 2025/4/19 12:13
 * @version: 1.0
 */
@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamDao examDao;

    @Autowired
    private QuestionDao questionDao;

    @Autowired
    private QuestionOptionDao questionOptionDao;

    @Autowired
    private PaperDao paperDao;

    @Autowired
    private ExamAnswerDao examAnswerDao;


    @Override
    public void saveAnswer(Map<String, Object> map) {
        String option_id =  map.get("option_id") == null? "" : (String) map.get("option_id");
        String question_id =  map.get("question_id") == null? "" : (String) map.get("question_id");
        String paper_id = map.get("paper_id") == null? "" : String.valueOf( map.get("paper_id").toString());
        String answer_content = map.get("answer_content") == null? "" : (String) map.get("answer_content");

        Paper paper = this.paperDao.getById(Long.parseLong(paper_id));
        Question question = this.questionDao.getById(Long.parseLong(question_id));
        QuestionOption questionOption = this.questionOptionDao.getById(Long.parseLong(option_id));

        ExamAnswer examAnswer = null;

        Specification<ExamAnswer> specification = (root, criteriaQuery, criteriaBuilder) -> {

            List<Predicate> predicates = new ArrayList<>();

            predicates.add(criteriaBuilder.equal(root.get("paper").get("id"), paper.getId()));
            predicates.add(criteriaBuilder.equal(root.get("question").get("id"), question.getId()));

            List<Long> questionOptionIds = new ArrayList<>();
            questionOptionIds.add(questionOption.getId());// 假设这是包含所有需要查询的 questionOption 的 id 的集合

            Join<ExamAnswer, QuestionOption> join = root.join("questionOptions");
            predicates.add(join.get("id").in(questionOptionIds));

            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));

        };

        List<ExamAnswer> all = this.examAnswerDao.findAll(specification);
        if (!all.isEmpty()) {
            examAnswer = all.get(0);
        } else {
            examAnswer = new ExamAnswer();
        }

        examAnswer.setPaper(paper);
        examAnswer.setQuestion(question);

        switch (question.getType()) {
            case SINGLE_CHOICE, JUDGMENT -> examAnswer.setQuestionOptions(List.of(questionOption));
            case MULTIPLE_CHOICE -> {

            }
            case SHORT_ANSWER -> examAnswer.setAnswer(answer_content);
        }

        examAnswerDao.save(examAnswer);

    }
}
